import axios from 'axios'
import { message } from 'ant-design-vue';
import userStorePinia from '@/store/modules/user'
let request = axios.create({
    baseURL: import.meta.env.VITE_SERVE + import.meta.env.VITE_APP_BASE_API,
    timeout: 5000
})

// 请求拦截器
request.interceptors.request.use((config) => {
    let userStore = userStorePinia()
    // 携带token
    config.headers.token = userStore.token
    return config
})

// 响应拦截器
request.interceptors.response.use((response) => {
    return response.data
}, (error) => {
    let msg = ''
    let status = error.response ? error.response.status : "ERR_NETWORK"
    switch (status) {
        case 400:
            msg = '请求参数错误'
            break;
        case 401:
            msg = 'Token过期'
            break;
        case 403:
            msg = '无权访问'
            break;
        case 404:
            msg = '路径错误'
            break;
        case 500:
            msg = '服务器错误'
            break;
        default:
            msg = '网络错误'
            break;

    }

    message.error(msg);

    return Promise.reject(error)
})

export default request